package org.hive.sequence
{
	import flash.utils.Dictionary;
	import org.hive.sequence.Sequence;
	import org.hive.state.State;
	import flash.events.EventDispatcher;
	
	public class Sequencer extends EventDispatcher
	{
        /**
        *
        */
		private var __SEQUENCES:Dictionary;

        /**
        *
        */
		private var __SEQUENCES_BY_NAME:Dictionary;

        /**
        *
        */
		private var __CURRENT_SEQUENCE:Sequence;

        /**
        *
        */
		private var __LAST_SEQUENCE:Sequence;
		
        /**
        *
        */
		public function Sequencer( )
		{
			__SEQUENCES 		= new Dictionary( );
			__SEQUENCES_BY_NAME	= new Dictionary( );
		}
		
        /**
        *
        * @param	sequence
        * @return
        */
		public function add( sequence:Sequence ):Sequence
		{
            if( __SEQUENCES[ __SEQUENCES_BY_NAME[ sequence.name ] ] )
            delete __SEQUENCES[ __SEQUENCES_BY_NAME[ sequence.name ] ];

			__SEQUENCES_BY_NAME[ sequence.name ] = sequence;
			__SEQUENCES[ sequence ] = sequence.name;
			
			return sequence;
		}
		
        /**
        *
        * @param	sequence
        * @return
        */
		public function remove( sequence:Sequence ):Sequence
		{
			delete __SEQUENCES[ sequence ];
			delete __SEQUENCES_BY_NAME[ sequence.name ];
			
			return sequence;
		}
		
        /**
        *
        * @param	name
        * @return
        */
		public function removeByName( name:String ):Sequence
		{
			return remove( getSequenceByName( name ) );
		}
		
        /**
        *
        * @param	name
        * @return
        */
		public function getSequenceByName( name:String ):Sequence
		{
			return __SEQUENCES_BY_NAME[ name ];
		}
		
        /**
        *
        * @param	sequence
        */
		public function set currentSequence( sequence:Sequence )
		{
			__LAST_SEQUENCE = __CURRENT_SEQUENCE;
			__CURRENT_SEQUENCE = sequence;
		}

        /**
        *
        * @return
        */
		public function get currentSequence( ):Sequence
		{
			return __CURRENT_SEQUENCE;
		}
		
        /**
        *
        * @param	sequence
        */
		public function set lastSequence( sequence:Sequence )
		{
			__LAST_SEQUENCE = sequence;
		}
		public function get lastSequence( ):Sequence
		{
			return __LAST_SEQUENCE;
		}
	}
}